﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.ArchiveMange.Models;
using System.Data;
using DreamBuilder.Core.Container;
using ePlanet.ArchiveMange.Services;
using ePlanet.ArchiveMange.Enum;
using Web.SystemConfig.Services;
using Microsoft.Practices.Unity;
using Wuqi.Webdiyer;

namespace Web.Views.ArchiveManage
{
    public partial class ArchiveGuidangList : PageBase
    {
        #region 属性
        /// <summary>
        /// 文档类型ID
        /// </summary>
        public int ArchiveTypeID
        {
            get
            {
                int o_int = 0;
                if (Request.QueryString["archivetype"] != null)
                {
                    if (int.TryParse(Request.QueryString["archivetype"], out o_int))
                    {
                        return o_int;
                    }
                    else
                    {
                        throw new Exception("档案类型错误");
                    }
                }
                else
                {
                    throw new Exception("档案类型不能为空");
                }
            }
        }

        [Dependency]
        public IArchiveMenuService ArchiveMenuService
        {
            get;
            set;
        }

        [Dependency]
        public ICodeTable CodeTableService
        {
            get;
            set;
        }

        [Dependency]
        public IAuth_User UserService
        { get; set; }
        #endregion

        #region 获取数据源



        private DataTable GetData()
        {
            int baseTypeId = ArchiveMenuService.GetArchiveBaseIDById(ArchiveTypeID);
            DataTable dt = null;
            switch (baseTypeId)
            {
                case ((int)ArchiveBaseType.Document):
                    IDocumentArchiveService document = UContainer.Resolve<IDocumentArchiveService>();
                    dt = document.GetArchiveByType(ArchiveTypeID.ToString());
                    break;
            }
            return dt;
        }
         private DataTable GetDoData()
        {
            //int baseTypeId = ArchiveMenuService.GetArchiveBaseIDById(ArchiveTypeID);
            //DataTable dt = null;
            //switch (baseTypeId)
            //{
            //    case ((int)ArchiveBaseType.Document):
            //        IDocumentArchiveService document = UContainer.Resolve<IDocumentArchiveService>();
            //        dt = document.GetAllDocumenArchivetData();
            //        break;
            //    case ((int)ArchiveBaseType.Account):
            //        IAccountArchiveService account = UContainer.Resolve<IAccountArchiveService>();
            //        dt = account.GetAllDocumenArchivetData();
            //        break;
            //}
            //return dt;
             return null;
        }
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.archiveTypeId.Value = this.ArchiveTypeID.ToString();
                BindGrid();
                BindDoGrid();
            }
        }

        #region gv_Archive事件
        protected void gr_Archive_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ArchiveMangeModal.DOCUMENT_ARCHIVERow row = ((DataRowView)e.Row.DataItem).Row as ArchiveMangeModal.DOCUMENT_ARCHIVERow;
                e.Row.Cells[2].Text = UserService.GetUserNameById(row.CREATEUSERID);
                e.Row.Cells[7].Text = CodeTableService.GetpkNameByCodepkValue(row.F_SECURITYTYPE);
            }
        }

        protected void pager_PageChanging(object sender, PageChangingEventArgs e)
        {
            pager.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void gr_Archive_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string pkValue = this.gv_Archive.DataKeys[e.NewEditIndex].Value.ToString();
            this.Response.Redirect("ArchiveEdit.aspx?archiveId=" + pkValue + "&archivetype=" + ArchiveTypeID + "");
        }

        protected void gv_Archive_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //string pkValue = e.Values[""];e.
            try
            {
                string pkValue = this.gv_Archive.DataKeys[e.RowIndex].Value.ToString();
                IDocumentArchiveService document = UContainer.Resolve<IDocumentArchiveService>();
                bool bReturn = document.DeleteData(pkValue);
                if (bReturn)
                {
                    Alert("删除成功！");
                    BindGrid();
                }
            }
            catch
            {
                Alert("操作出现异常，请重新再试！");
            }
        }
        #endregion
        #region gv_Archive_do事件
        protected void gr_Archive_do_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ArchiveMangeModal.DOCUMENT_ARCHIVERow row = ((DataRowView)e.Row.DataItem).Row as ArchiveMangeModal.DOCUMENT_ARCHIVERow;
                e.Row.Cells[1].Text = UserService.GetUserNameById(row.CREATEUSERID);
                e.Row.Cells[6].Text = CodeTableService.GetpkNameByCodepkValue(row.F_SECURITYTYPE);
            }
        }

        protected void pagerdo_PageChanging(object sender, PageChangingEventArgs e)
        {
            pagerdo.CurrentPageIndex = e.NewPageIndex;
            BindDoGrid();
        }
        #endregion
        #endregion

        #region  私有方法
        /// <summary>
        /// 绑定GridView
        /// </summary>
        private void BindGrid()
        {
            int recordCount = 0;
            DataTable dt = GetData();
            this.gv_Archive.DataSource = PageDataList.GetPagedTable(dt, pager.CurrentPageIndex, pager.PageSize, out recordCount);
            pager.RecordCount =recordCount;
            this.gv_Archive.DataBind();
        }

        private void BindDoGrid()
        {
            int recordCount = 0;
            DataTable dt = GetDoData();
            this.gv_Archive_do.DataSource = PageDataList.GetPagedTable(dt, pagerdo.CurrentPageIndex, pagerdo.PageSize,out recordCount);
            pagerdo.RecordCount =recordCount;
            this.gv_Archive_do.DataBind();
        }

        #endregion
    }
}